package com.walterweimod.auxiliary_attributes.event;

import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeLivingEntityAttributeRangedValueBase;
import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.commons.lang3.ObjectUtils;

import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.getLogger;
import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.getCapabilityValue;

@Mod.EventBusSubscriber
public class RageDepthEvent {
    @SubscribeEvent(priority = EventPriority.LOW)
    public static void onLivingEntityBeingHurt(LivingHurtEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance rage_depth = livingEntity.getEffect(ModMobEffects.RAGE_DEPTH.get());
        MobEffectInstance rage = livingEntity.getEffect(ModMobEffects.RAGE.get());

        if (ObjectUtils.allNotNull(rage, rage_depth)){
            Integer rage_level = getCapabilityValue(
                    livingEntity,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeLivingEntityAttributeRangedValueBase::getValue,
                    0
            );

            event.setAmount(event.getAmount() * Math.max(
                    1.0F - 0.01F * rage_level, Math.max(
                            0.65F - 0.01F * rage_depth.getAmplifier(), 0.05F
                    )
            ));

            getLogger().info("触发预兆性景深，战意上限为：{}", getCapabilityValue(
                    livingEntity,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeLivingEntityAttributeRangedValueBase::getMaxValue,
                    0
            ));

            getLogger().info("触发预兆性景深，生命值降低百分比为：{}", Math.max(
                    1.0F - 0.01F * rage_level, Math.max(
                            0.65F - 0.01F * rage_depth.getAmplifier(), 0.05F
                    )
            ));
        }
    }
}
